﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.NodeFactory;
using Vega.USiteBuilder;
using SPCFMedicineInformer.DocumentTypes;
using SPCFMedicineInformer.Helpers;

namespace SPCFMedicineInformer.Masterpages
{
    public partial class ContentMedicineDetails : System.Web.UI.MasterPage
    {
        public string GenericName { get; set; }
        public string CategoryName { get; set; }
        public string MedicineName { get; set; }
        public int GenId { get; set; }
        public int MedId { get; set; }
        public int CatId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CategoryName = Request["catName"] != null ? Request["catName"].ToString() : "";
                GenericName = Request["genName"] != null ? Request["genName"].ToString() : "";
                MedicineName = Request["medName"] != null ? Request["medName"].ToString() : "";
                int tempInt;
                GenId = int.TryParse(GenericName, out tempInt) ? tempInt : 0;
                MedId = int.TryParse(MedicineName, out tempInt) ? tempInt : 0;
                CatId = int.TryParse(CategoryName, out tempInt) ? tempInt : 0;
                bool hasRecord = true;

                
                Node categoryNode = new Node(CatId);
                if (categoryNode == null)
                {
                    hasRecord = false;
                }
                Node genericNameNode = new Node(GenId);
                if (genericNameNode == null)
                {
                    hasRecord = false;
                }

                Node medicineNode = new Node(MedId);
                if (medicineNode != null
                    && hasRecord
                    && !string.IsNullOrEmpty(medicineNode.Name)
                    && medicineNode.NodeTypeAlias == "MedicineItem"
                    && medicineNode.Parent.Id == genericNameNode.Id
                    && medicineNode.Parent.Parent.Id == categoryNode.Id
                    )
                {
                    CategoryName = categoryNode.Name;
                    GenericName = genericNameNode.Name;
                    MedicineName = medicineNode.Name;
                    var Medicine = ContentHelper.GetByNodeId<MedicineItem>(medicineNode.Id);
                    TransferDetailsToPage(Medicine);
                    ShowContent(true);
                }
                else
                {
                    ShowContent(false);
                }

            }
            
        }
        void ShowContent(bool b)
        {
            plcContent.Visible = b;
            plcError.Visible = !b;
        }

        void TransferDetailsToPage(MedicineItem m)
        {
            litAdverseReactions.Text = !string.IsNullOrEmpty(m.AdverseReactionsMT) ? m.AdverseReactionsMT : m.AdverseReactions.ToString();
            litContradictions.Text = !string.IsNullOrEmpty(m.ContradictionMT) ? m.ContradictionMT : m.Contradiction;
            litDosages.Text = m.Dosages;
            litGenericName.Text = FormHelper.ParseIdStringToNodeName(m.GenericId);
            litIndications.Text = m.Indication;
            litPregnancySafety.Text = m.PregnancySafety;
            litPresentation_Packaging.Text = !string.IsNullOrEmpty(m.Presentation_PackagingMT) ? m.Presentation_PackagingMT : m.Presentation_Packaging;
            litSpecialPrecautions.Text = m.SpecialPrecautions;
            litDrugInteraction.Text = m.DrugInteraction;
            string imgUrl = !string.IsNullOrEmpty(m.ImageUrl) ? m.ImageUrl : "/images/web-blue/default_medicine_image.jpg";
            string file = imgUrl.Substring(imgUrl.LastIndexOf('/') + 1);
            img.ImageUrl = imgUrl;
            img.AlternateText = file;
            img.ToolTip = file;
            linkImg.NavigateUrl = imgUrl;
            linkImg.ToolTip = m.MedicineName;
            litAdministration.Text = m.Administration;
            HideRows(m);
        }

        void HideRows(MedicineItem m)
        {
            rowAdministration.Visible = !string.IsNullOrEmpty(m.Administration);
            rowAdverseReactions.Visible = !string.IsNullOrEmpty(m.AdverseReactionsMT + m.AdverseReactions);
            rowContradictions.Visible = !string.IsNullOrEmpty(m.Contradiction + m.ContradictionMT);
            rowDosages.Visible = !string.IsNullOrEmpty(m.Dosages);
            rowDrugInteraction.Visible = !string.IsNullOrEmpty(m.DrugInteraction);
            rowIndication.Visible = !string.IsNullOrEmpty(m.Indication);
            rowPregnancySafety.Visible = !string.IsNullOrEmpty(m.PregnancySafety + m.PregnancySafetyMT);
            rowPresentation.Visible = !string.IsNullOrEmpty(m.Presentation_Packaging + m.Presentation_PackagingMT);
            rowSpecialPrecautions.Visible = !string.IsNullOrEmpty(m.SpecialPrecautions);
        }
    }
}